package 每日一题;

import java.util.HashSet;

public class 重复叠加字符串匹配 {
    public int repeatedStringMatch(String a, String b) {
        int num = b.length()/a.length();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < num; i++) {
            builder.append(a);
        }
        if (isPart(builder.toString(),b))
            return num;
        if (isPart(builder.append(a).toString(),b))
            return num+1;
        if (isPart(builder.append(a).toString(),b))
            return num+2;
        return -1;
    }
    //判断b是不是a的子串
    boolean isPart(String a,String b){
        int n = b.length();
        for (int i = 0; i <= a.length() - n; i++) {
            String sub = a.substring(i, i + n);
            if (sub.equals(b))
                return true;
        }
        return false;
    }
}
